# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def getDecimalValue(self, head: Optional[ListNode]) -> int:
        pre = None
        cur = head
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt

        ret = 0
        idx = 0
        while pre:
            if pre.val == 0:
                idx += 1
                pre = pre.next
                continue
            ret += pow(2, idx)
            idx = idx + 1
            pre = pre.next
        return ret
